home *** CD-ROM | disk | FTP | other *** search
- /* Calculate random Normal Deviates */
- options results
- if ~show('P','TCALC') then do
- address command 'run turbocalc:turbocalc'
- address command 'waitforport TCALC'
- loadflag=1
- end
- address 'TCALC'
- 'DEFPUBSCREEN()'
- /* Add-in Rexx Libraries needed for some routines */
- signal on syntax
- if ~show('l','rexxmathlib.library') then
- call addlib('rexxmathlib.library',0,-30) /* add to library list */
- if ~show('l','rexxreqtools.library') then
- call addlib('rexxreqtools.library',0,-30)
- if ~show('l','rexxsupport.library') then
- call addlib('rexxsupport.library',0,-30)
- signal off syntax
-
- /* Start Main Routine */
- if loadflag=1 then 'Load()'
- 'ActivateWindow()'
- NCols=rtgetlong("0","Enter number of columns to provide","Input Request") /*,,'rt_pubscrname="TCALC"')*/
- if rtresult=0|NCols=0 then do
- 'Message "Aborting!"'
- 'DEFPUBSCREEN("Workbench")'
- exit
- end
- NRows=rtgetlong("0","Enter number of rows to provide","Input Request") /*,,'rt_pubscrname="TCALC"')*/
- if rtresult=0|NRows=0 then do
- 'Message "Aborting!"'
- 'DEFPUBSCREEN("Workbench")'
- exit
- end
- /* Get cell reference for output range */
- out_cell=rtgetstring(,"Enter Cell Reference for Output","Input Request") /*,,'rt_pubscrname="TCALC"')*/
- if out_cell="" then do
- 'DEFPUBSCREEN("Workbench")'
- exit
- end
- if length(out_cell)<2 | datatype(left(out_cell,1),'n') then do
- 'Message "Invalid cell reference"'
- 'DEFPUBSCREEN("Workbench")'
- exit
- end
- /* Suppress Screen Redraw to Speed Things Up */
- 'Refresh 0'
-
- /* Open a small output window on tcalc screen*/
- fo=0
- CR='0a'x
- DisplayMsg="Calculating...Please Wait."||CR||"User input is disabled during calculation."||CR
- if open(6Info, 'con:100/0/450/80/Progress/SCREEN TCALC', w) then do
- call writeln(6Info, DisplayMsg)
- fo=1
- end
- else do
- 'Message "TCALC Screen not available for Progress messages"'
- end
- CALL DELAY(150)
-
- /* Create data */
- RN.=0
- Call RANDN(NCols,NRows)
- call writeln(6Info,"Writing output to window...")
-
- /* Output */
- 'SelectCell' out_cell
- 'ColumnWidth 10'
- 'Put "Table of Random Normal Deviates with zero Mean and unit Variance"'
- 'CursorDown 1'
- 'GetCursorPos'
- top_cell=result
- Do x=1 to NCols
- Do y=1 to NRows
- 'Put' RN.x.y
- 'CursorDown 1'
- end
- 'SelectCell' top_cell
- 'Column' x
- end
- 'SelectCell' out_cell
- 'CursorDown' NRows+3
- 'Put "Calculated using the ratio of uniforms method"'
- 'CursorDown 1'
- 'Put "of A.J. Kinderman and J.F. Monahan"'
- 'CursorDown 1'
- 'Put "augmented with quadratic bounding curves."'
- 'CursorDown 1'
- 'Put "ADAPTED FROM WORK PUBLISHED IN TRANSACTIONS ON MATHEMATICAL SOFTWARE,"'
- 'CursorDown 1'
- 'Put "VOL. 18, NO. 4, DECEMBER, 1992, PP. 434-435."'
- 'Refresh 1'
- 'Refresh 2'
- DisplayMsg="Cleaning up ...."||CR||"Exiting"
- result=writeln(6Info, DisplayMsg)
- if result~=0 then do
- /*Wait 3 seconds*/
- CALL DELAY(150)
- /* close window*/
- result=close(6Info)
- end
- 'DEFPUBSCREEN("Workbench")'
- exit
-
- /* Procedures */
-
- RANDN: Procedure Expose RN.
-
- arg N,R
- S=0.449871
- T=-0.386595
- A=0.19600
- B=0.25472
- R1=0.27597
- R2=0.27846
- Do i=1 to N
- Do j=1 to R
- Do forever
- U = RANDU(time('s'))
- V = RANDU(time('s'))
- V = 1.7156 * (V - 0.5)
- /*Evaluate the quadratic form*/
- X = U - S
- Y = ABS(V) - T
- Q = X**2 + Y*(A*Y - B*X)
- /*Accept P if inside inner ellipse*/
- IF (Q < R1) Then Leave
- /*Reject P if outside outer ellipse*/
- IF (Q > R2) Then Iterate
- /*Reject P if outside acceptance region*/
- IF (V**2 > -4.0*LOG(U)*U**2) Then Iterate
- End
- RN.i.j=V/U
- End
- End
- Return
-
- /* ALGORITHM 712, COLLECTED ALGORITHMS FROM ACM.*/
- /* THIS WORK PUBLISHED IN TRANSACTIONS ON MATHEMATICAL SOFTWARE,
- VOL. 18, NO. 4, DECEMBER, 1992, PP. 434-435.
- The function RANDN() returns a normally distributed pseudo-random
- number with zero mean and unit variance. Calls are made to a
- function subprogram RANDU() which must return independent random
- numbers uniform in the interval (0,1).
-
- The algorithm uses the ratio of uniforms method of A.J. Kinderman
- and J.F. Monahan augmented with quadratic bounding curves.
- */